<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <ui:composition template="/template/DefaultTemplate.xhtml">
        <ui:define name="title">
            <h:outputText value="Create Sales Order"></h:outputText>
        </ui:define>
        <ui:define name="content">
            <h:form rendered="#{loginBacking.addSalesOrder}">
                <p:tabView>
                        <p:messages />
                        <br />
                        <p:tab title="Create Sales Order">
                            <p:panel header="Create Sales Order">
                            <p:fieldset legend="Information">
                                <h:panelGrid columns="4">
                                    <h:outputLabel value="#{bundle.CreateSalesOrderEntityLabel_purchaseOrder}" for="purchaseOrder" />
                                    <p:commandLink id="purchaseOrder" ajax="false" value="#{salesOrderEntityController.selected.purchaseOrder.id}" action="#{purchaseOrderEntityController.viewPOFromSO}" />
                                    <h:outputText value=" "/>
                                    <h:outputText value=" "/>
                                    <h:outputLabel value="#{bundle.CreateSalesOrderEntityLabel_customer}" for="customer" />
                                    <p:commandLink id="customer" ajax="false" value="#{salesOrderEntityController.selected.customer.firstName}&nbsp;#{salesOrderEntityController.selected.customer.lastName}" action="#{customerEntityController.viewCustomerFromSO}"/>
                                    <h:selectBooleanCheckbox id="checkbox2" value="#{salesOrderEntityController.selected.customer.blackListed}" disabled="true"/>
                                    <h:outputText value="Blacklisted"/>
                                    <h:outputLabel value="Customer Executive:" for="systemUser" />
                                    <h:outputText id="systemUser" value="#{salesOrderEntityController.selected.systemUser.firstName}&nbsp;#{salesOrderEntityController.selected.systemUser.lastName}" title="#{bundle.CreateSalesOrderEntityTitle_systemUser}" />
                                </h:panelGrid>
                            </p:fieldset>
                            <br />
                            <h:selectBooleanCheckbox id="checkbox" value="#{salesOrderEntityController.selected.promotion}"/>
                            <h:outputText value="Promotion"/>
                            <br />
                            <p:dataTable value="#{purchaseOrderEntityController.selected.productQuantityPrice}" var="pqp" style="border:solid 1px">
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Product"/>
                                    </f:facet>
                                    <h:outputText value="#{pqp.product.productName}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Quantity (Number Of Cases)"/>
                                    </f:facet>
                                    <h:outputText value="#{pqp.quantity}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Price Per Unit (S$)"/>
                                    </f:facet>
                                    <h:outputText value="#{pqp.pricePerUnit}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="Sub Total Price (S$)"/>
                                    </f:facet>
                                    <h:outputText value="#{pqp.price}"/>
                                </p:column>
                                
                                <p:columnGroup type="footer">  
                                    <p:row>  
                                        <p:column colspan="3" footerText="Total Price:"  
                                                  style="text-align:left"  />  

                                        <p:column footerText="S$ #{salesOrderEntityController.selected.totalPrice}" style="text-align:left" />  

                                    </p:row>  
                                </p:columnGroup>
                            </p:dataTable>
                            </p:panel>
                        </p:tab>
                        <p:tab title="View Corresponding Purchase Order">
                                <p:panel header="View Purchase Order # #{purchaseOrderEntityController.selected.id}">
                                    <p:messages />
                                    <br />
                                    <p:tabView>
                                        <p:tab title="Purchase Order">
                                            <h:panelGrid columns="6">
                                                <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_customer}"/>
                                                <p:commandLink ajax="false" value="#{purchaseOrderEntityController.selected.customer.firstName}&nbsp;#{purchaseOrderEntityController.selected.customer.lastName}" action="#{customerEntityController.viewCustomerFromPO}" title="#{bundle.ViewPurchaseOrderEntityTitle_customer}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="Customer Id:"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.customer}" title="#{bundle.ViewPurchaseOrderEntityTitle_customer}"/>
                                                <h:outputText value="Customer Executive:"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.customer.customerExecutive.firstName}&nbsp;#{purchaseOrderEntityController.selected.customer.customerExecutive.lastName}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_salesQuotation}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation}" title="#{bundle.ViewPurchaseOrderEntityTitle_salesQuotation}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_salesOrder}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesOrder}" title="#{bundle.ViewPurchaseOrderEntityTitle_salesOrder}" rendered="#{purchaseOrderEntityController.selected.salesOrder!=null}"/>
                                                <h:outputText value="*Sales Order Not Created" style="color:red" title="#{bundle.ViewPurchaseOrderEntityTitle_salesOrder}" rendered="#{purchaseOrderEntityController.selected.salesOrder==null}"/>
                                            </h:panelGrid>
                                            <br />
                                            <p:separator/>
                                            <br />
                                            <p:fieldset legend="Customer Preference">
                                                <h:panelGrid columns="2">
                                                    <h:outputText value="Customer's preferred shipping date:"/>
                                                    <h:outputText value="#{purchaseOrderEntityController.selected.preferredLeadTime}" title="#{bundle.ViewPurchaseOrderEntityTitle_preferredLeadTime}">
                                                        <f:convertDateTime pattern="dd/MM/yyyy" />
                                                    </h:outputText>
                                                </h:panelGrid>
                                            </p:fieldset>
                                            <br />
                                            <p:dataTable value="#{purchaseOrderEntityController.selected.productQuantityPrice}" var="pqp" style="border:solid 1px">
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Product"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.product.productName}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Quantity"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.quantity}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Price Per Unit (S$)"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.pricePerUnit}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Sub Total Price (S$)"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.price}"/>
                                                </p:column>
                                                <p:columnGroup type="footer">  
                                                    <p:row>  
                                                        <p:column colspan="3" footerText="Total Price:"  
                                                                  style="text-align:left"  />  

                                                        <p:column footerText="S$ #{purchaseOrderEntityController.selected.totalPrice}" style="text-align:left" />  

                                                    </p:row>  
                                                </p:columnGroup>
                                            </p:dataTable>
                                        </p:tab>
                                        <p:tab title="Sales Quotation">
                                            <h:panelGrid columns="6">
                                                <h:outputText value="Price Validity:" />
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.validity}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.validity!=null}"/>
                                                <h:outputText value="*Validity Period Not Set*" style="color:red" rendered="#{purchaseOrderEntityController.selected.salesQuotation.validity==null}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="System User:" />
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.systemUser.firstName}&nbsp;#{purchaseOrderEntityController.selected.salesQuotation.systemUser.lastName}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.systemUser!=null}"/>
                                                <h:outputText value="*Price Not Quoted*" style="color:red" rendered="#{purchaseOrderEntityController.selected.salesQuotation.systemUser==null}"/>

                                                <h:outputText value="Status:"/>
                                                <h:outputText value="Invalid" rendered="#{purchaseOrderEntityController.selected.salesQuotation.valid==false}"/>
                                                <h:outputText value="Valid" rendered="#{purchaseOrderEntityController.selected.salesQuotation.valid==true}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="Indicative Lead Time:"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.indicativeLeadTime}" title="#{bundle.ViewSalesQuotationEntityTitle_indicativeLeadTime}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.indicativeLeadTime!=null}"/>
                                                <h:outputText value="*Date Not Given*" style="color:red" rendered="#{purchaseOrderEntityController.selected.salesQuotation.indicativeLeadTime==null}"/>

                                                <h:outputText value="#{bundle.ViewSalesQuotationEntityLabel_customer}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}"/>
                                                <p:commandLink ajax="false" value="#{purchaseOrderEntityController.selected.salesQuotation.customer.firstName}&nbsp;#{purchaseOrderEntityController.selected.salesQuotation.customer.lastName}" action="#{customerEntityController.viewCustomerFromPO}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}"/>
                                                <h:outputText value="" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}"/>
                                                <h:outputText value="" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}"/>
                                                <h:outputText value="Customer Id:" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}" />
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.customer}" rendered="#{purchaseOrderEntityController.selected.salesQuotation.customer!=null}"/>

                                                <h:outputText value="#{bundle.ViewSalesQuotationEntityLabel_createdBy}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.createdBy.firstName}&nbsp;#{purchaseOrderEntityController.selected.salesQuotation.createdBy.lastName}" title="#{bundle.ViewSalesQuotationEntityTitle_createdBy}"/>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="#{bundle.ViewSalesQuotationEntityLabel_lastModifiedBy}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.lastModifiedBy.firstName}&nbsp;#{purchaseOrderEntityController.selected.salesQuotation.lastModifiedBy.lastName}" title="#{bundle.ViewSalesQuotationEntityTitle_lastModifiedBy}"/>

                                                <h:outputText value="#{bundle.ViewSalesQuotationEntityLabel_createdAt}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.createdAt}" title="#{bundle.ViewSalesQuotationEntityTitle_createdAt}">
                                                    <f:convertDateTime pattern="dd/MM/yyyy" />
                                                </h:outputText>
                                                <h:outputText value="" />
                                                <h:outputText value="" />
                                                <h:outputText value="#{bundle.ViewSalesQuotationEntityLabel_lastModifiedAt}"/>
                                                <h:outputText value="#{purchaseOrderEntityController.selected.salesQuotation.lastModifiedAt}" title="#{bundle.ViewSalesQuotationEntityTitle_lastModifiedAt}">
                                                    <f:convertDateTime pattern="dd/MM/yyyy" />
                                                </h:outputText>

                                            </h:panelGrid>
                                            <p:dataTable value="#{purchaseOrderEntityController.selected.salesQuotation.pqp}" var="pqp" style="border:solid 1px">
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Product"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.product.productName}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Quantity"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.quantity}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Price Per Unit (S$)"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.pricePerUnit}"/>
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">
                                                        <h:outputText value="Sub Total Price (S$)"/>
                                                    </f:facet>
                                                    <h:outputText value="#{pqp.price}"/>
                                                </p:column>
                                                <p:columnGroup type="footer">  
                                                    <p:row>  
                                                        <p:column colspan="3" footerText="Total Price:"  
                                                                  style="text-align:left"  />  

                                                        <p:column footerText="S$ #{purchaseOrderEntityController.selected.salesQuotation.totalPrice}" style="text-align:left" />  

                                                    </p:row>  
                                                </p:columnGroup>
                                            </p:dataTable>
                                        </p:tab>
                                        <br />
                                </p:tabView>
                                    </p:panel>
                                    <br />
                                    <ui:include src="/entities/shippingRequestEntity/ListShippingRequests.xhtml" />
                                    <br />
                                    <p:panel header="Payment Information">
                                        <h:panelGrid columns="2">
                                            <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_paymentMethod}"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.paymentMethod}" title="#{bundle.ViewPurchaseOrderEntityTitle_paymentMethod}"/>
                                            <h:outputText value="Billing Address:"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.billingAddress.toString()}"/>
                                        </h:panelGrid>
                                    </p:panel>
                                    <br />
                                    <p:panel header="Meta Information">
                                        <h:panelGrid columns="6">
                                            <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_createdBy}"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.createdBy.firstName}&nbsp;#{purchaseOrderEntityController.selected.createdBy.lastName}" title="#{bundle.ViewPurchaseOrderEntityTitle_createdBy}"/>
                                            <h:outputText value="" />
                                            <h:outputText value="" />
                                            <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_lastModifiedBy}"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.lastModifiedBy.firstName}&nbsp;#{purchaseOrderEntityController.selected.lastModifiedBy.lastName}" title="#{bundle.ViewPurchaseOrderEntityTitle_lastModifiedBy}"/>
                                            <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_createdAt}"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.createdAt}" title="#{bundle.ViewPurchaseOrderEntityTitle_createdAt}">
                                                <f:convertDateTime pattern="dd/MM/yyyy" />
                                            </h:outputText>
                                            <h:outputText value="" />
                                            <h:outputText value="" />
                                            <h:outputText value="#{bundle.ViewPurchaseOrderEntityLabel_lastModifiedAt}"/>
                                            <h:outputText value="#{purchaseOrderEntityController.selected.lastModifiedAt}" title="#{bundle.ViewPurchaseOrderEntityTitle_lastModifiedAt}">
                                                <f:convertDateTime pattern="dd/MM/yyyy" />
                                            </h:outputText>  
                                        </h:panelGrid>
                                    </p:panel>
                                    <br />
                                    <p:panel header="Service Request(s)" toggleable="true" toggleSpeed="500" collapsed="true" rendered="#{salesQuotationEntityController.checkSystemUser()==true}">
                                        <p:dataTable value="#{purchaseOrderEntityController.selected.serviceRequests}" 
                                                     var="sr" style="table-layout:fixed"
                                                     paginator="true" rows="10"  
                                                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                                                     rowsPerPageTemplate="5,10,15">
                                            <p:column>
                                                <f:facet name="header">
                                                    <h:outputText value="Service Request"/>
                                                </f:facet>
                                                <h:outputText value="#{sr}"/>
                                            </p:column>
                                        </p:dataTable>
                                    </p:panel>
                        </p:tab>
                </p:tabView>
                <p:commandButton ajax="false" action="#{salesOrderEntityController.create}" value="Create" />
                &nbsp;
                <p:commandButton ajax="false" action="#{purchaseOrderEntityController.viewPOFromSO}" value="Cancel" immediate="true"/>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
